// pages/tabbar/index/product/index.js
const App = getApp();
const {Dialog, Toast} = require("../../../../../config/modal")
const utils = require("../../../../../utils/util")
const {LOGIN, LOCATION} = require("../../../../../config/login.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: '物品详情',
        capsuleBarHeight: App.$getMenuButtonHeight(),
        offsetTop: App.globalData.window.navHeight,
        theamTextColor: App.globalData.theamTextColor,
        userInfo: {},
        userConcat: {
            show: false,
        },
        product: {},
    },
    URL: {
        detail: '/product/detail/?',
        view: '/product/view/?',
        userConcatInfo: '/customer/userConcatInfo/?',
    },
    page: {
        home: '/pages/tabbar/index/index',
        detail: "/pages/tabbar/index/product/detail/index"
    },

    onLoad: function (options) {
        // 检查是否被邀请进来的
        let productNo= utils.isInvalid(options.productNo)?"":options.productNo
        this.setData({
            'product.productNo':productNo
        })

        App.$handlerInviteCode(options);

        this.getData(productNo);

        this.toView(productNo);
    },

    getData(productNo) {
        if (utils.isInvalid(productNo)){
            return;
        }
        App.$get(true, this.URL.detail.replace("?", productNo), {}, res => {
            if (res.R) {
                this.setData({
                    product: res.data
                })
            }
        })
    },


    toView(productNo) {
        if (utils.isInvalid(productNo)){
            return;
        }
        App.$postIsTip(true, false, this.URL.view.replace("?", productNo), {}, res => {
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    showLagerImg(e) {
        let imgUrl = e.currentTarget.dataset.img;
        let imageList = this.data.product.imageList;
        let imgs=[]
        imageList.forEach(e=>{
            imgs.push(e.imgUrl)
        })
        wx.previewImage({
            current: imgUrl,
            urls: imgs
        })
    },

    getUserConcat(e) {
        let userCode = this.data.product.userCode;
        let userConcat = this.data.userConcat[userCode];
        if (utils.isInvalid(userConcat)) {
            App.$get(true, this.URL.userConcatInfo.replace("?", userCode), {}, res => {
                if (res.R) {
                    let userCodeKey = "userConcat." + userCode;
                    this.setData({
                        'userConcat.item': res.data,
                        [userCodeKey]: res.data
                    })
                    this.setData({
                        'userConcat.show': true
                    })
                }
            })
            return;
        }
        this.setData({
            'userConcat.item': userConcat,
        })
        this.setData({
            'userConcat.show': true
        })
    },
    closeUserInfo() {
        this.setData({
            'userConcat.show': false
        })
    },


    goBack() {
        wx.navigateBack();
    },
    goHome() {
        App.$switchTab(this.page.home, false)
    },

    makePhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.userphone
        })
    },
    copyWechat(e) {
        let wechat = e.currentTarget.dataset.wechat;
        wx.setClipboardData({
            data: wechat,
            success: function (res) {
                wx.getClipboardData({
                    success: function () {
                        wx.showToast({
                            title: '已复制',
                            icon:'none'
                        })
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.stopPullDownRefresh();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return App.$onShare('/pages/tabbar/index/product/detail/index', "&productNo="+this.data.product.productNo, "物品详情")
    }
})
